package com.servlet;

import com.pojo.Booking;
import com.service.BookingService;
import com.service.impl.BookingServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = {"/booking"})
public class BookingServlet extends HttpServlet {
    BookingService bookingService = new BookingServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        String opr = request.getParameter("opr");

        if ("list".equals(opr) || opr == null){
            request.getSession().setAttribute("list",bookingService.findAllBooking());
            response.sendRedirect("list.jsp");
        }else if ("add".equals(opr)){
            String name = request.getParameter("name");
            String cardId = request.getParameter("cardId");
            String date = request.getParameter("date");

            Booking booking = new Booking();
            booking.setUserName(name);
            booking.setCardId(cardId);
            booking.setBookDate(date);

            int count = bookingService.addBooking(booking);
            if (count > 0){
                request.getSession().setAttribute("msg","添加预约人" + "-" + booking.getUserName() + "-" + "成功");
                response.sendRedirect("booking");
            }else {
                request.getSession().setAttribute("msg","添加预约人" + "-" + booking.getUserName() + "-" + "失败");
                response.sendRedirect("booking");
            }
        }else if ("edit".equals(opr)){
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            Booking booking = new Booking();
            booking.setId(Integer.parseInt(id));
            booking.setUserName(name);

            int count = bookingService.updateBooking(booking);
            if (count > 0){
                request.getSession().setAttribute("msg","已完成" + "-" + booking.getUserName() + "-" + "的疫苗接种");
                response.sendRedirect("booking");
            }else {
                request.getSession().setAttribute("msg","未完成" + "-" + booking.getUserName() + "-" + "的疫苗接种");
                response.sendRedirect("booking");
            }
        }else if ("getBooking".equals(opr)){
            String cardNum = request.getParameter("cardNum");
            Booking booking = new Booking();
            booking.setCardId(cardNum);
            if (bookingService.isBookingExist(booking)){
                out.print("true");
            }else {
                out.print("false");
            }
        }

        out.close();
    }
}
